package com.scommunity.uniapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scommunity.common.ResponseResult;
import com.scommunity.domain.Car;
import com.scommunity.domain.Owner;
import com.scommunity.domain.Parking;
import com.scommunity.domain.User;
import com.scommunity.service.ICarService;
import com.scommunity.service.IOwnerService;
import com.scommunity.service.IParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.List;

@RestController
@RequestMapping("/parking")
public class UParkingController {
    @Autowired
    private IParkingService iParkingService;
    @Autowired
    private IOwnerService iOwnerService;

    @GetMapping("/all")
    public Object all(@SessionAttribute User user){
        Long ownerId = getOwnerId(user);
        Car car = iParkingService.findCarByOwnerId(ownerId);
        Long carId = car.getCarId();
        List<Parking> list = iParkingService.getByCarId(carId);
        return ResponseResult.success(list);
    }

    public Long getOwnerId(User user){
        Long userId = user.getUserId();
        Owner owner = iOwnerService.getOne(new QueryWrapper<Owner>().eq("user_id", userId));
        Long ownerId = owner.getOwnerId();
        return ownerId;
    }
}
